#include "stm32f1xx_hal.h"
#include "iap.h"
#include "main.h"


pFunction Jump_To_Application;
uint32_t JumpAddress;

void Jump2App(void)
{
    /* Test if user code is programmed starting from address "APPLICATION_ADDRESS" */
    if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
    { 
      /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
      Jump_To_Application = (pFunction) JumpAddress;
      /* Initialize user application's Stack Pointer */
      __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
      Jump_To_Application();
    }
}

void BufProgram2Flash(uint32_t *pbuf, uint32_t wordlen, uint32_t addr)
{
    uint32_t i;

    HAL_FLASH_Unlock();

    for ( i = 0; i < wordlen; i++)
    {
        HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr, *pbuf);
        addr += 4;
        pbuf++;
    }

    HAL_FLASH_Lock();
}



